<?php
namespace Home\Controller;
use Think\Controller;
class DeviceController extends Controller {
    public function index(){
    	echo "Device";
    }

    public function addToken(){
    	$db_circle = M('Device');
    	$deviceid = I('deviceid');
    	$token = I('token');
    	$res = $db_circle->where("Device_ID = '%d'", 4)->setField('token', $token);
    	echo json_encode($res);
    }

    public function login(){
/*		$username = $_POST["username"];
		$password = $_POST["password"];*/

		$username = I('username');
		$password = md5(I('password'));
		if($username!="" && $password!="")
		{
			session_start();
			$db_administrator = M('User');
			$condition['User_name'] = $username;
			$res = $db_administrator->where($condition)->select();
			
	    	if($password == $res[0]['User_password']) {
	    		session('username', $username);

	    		$array= array('success'=>1, 'userid'=>$res[0]['User_id'], 'username'=>$res[0]['User_name']);
	    		echo json_encode($array);
	 		}else {
	 			//$array = array('oldpassword'=>$res[0]['password'], 'newpassword'=>$password);
	    		echo '{"success":0,"error":"用户名/密码错误."}';
				//echo json_encode($array);
			}
		}else{
			echo '{"success":0,"error":"用户名/密码未填写"}';
		}
	}

	public function logout(){
		session_start();
		session_unset();
		session_destroy();
	}

}